package com.dogkr.server.web;

import java.io.IOException;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.dogkr.common.Constants;
import com.dogkr.common.ServerAction;
import com.dogkr.server.entity.HostPhoto;
import com.dogkr.server.entity.HostPhotoAlbum;
import com.dogkr.server.service.PhotoService;


// 设置Struts2注解
//@ParentPackage(value = "login-default")
@Namespace(value = "/photo")
@ResultPath("/WEB-INF/jsps")
// 页面文件空间
@Results({
	@Result(name = "Photos", location = "photos.jsp"),
	@Result(name = "AlbumEdit", location = "album_edit.jsp")
})
public class PhotoAction extends ServerAction {
	private static final long serialVersionUID = 1L;
	
	private HostPhoto photo;
	
	private HostPhotoAlbum album;
	
	@Autowired
	private PhotoService photoService;
	
	
	
	@Action(value="photos")
	public String photos() {
		HostPhotoAlbum album = photoService.selectPhotoAlbumByKey(id);
		if(album.getHostId() == this.getLoginUser().getHost().getId()) {
			put("album", album);
			put("photos", photoService.selectPhotoByAlbumKey(album.getId()));
		}
		put("fileSize", Constants.UPLOAD_FILE_SIZE);
		return "Photos";
	}
	
	@Action(value="album_edit")
	public String albumEdit() {
		if(id != null) {
			HostPhotoAlbum album = photoService.selectPhotoAlbumByKey(id);
			if(album.getHostId() == this.getLoginUser().getHost().getId()) {
				put("album", album);
			}
		}
		return "AlbumEdit";
	}
	
	//店铺图片列表
	@Action(value="host")
	public void hostPhoto() {
		outJson(photoService.selectPhotoByHostKey(this.getLoginUser().getHost().getId()));
	}
	
	//店铺相册列表
	@Action(value="album")
	public void hostAlbum() {
		outJson(photoService.selectPhotoAlbumByHostKey(this.getLoginUser().getHost().getId()));
	}
	//相册图片列表
	@Action(value="list")
	public void albumPhoto() {
		HostPhotoAlbum album = photoService.selectPhotoAlbumByKey(id);
		if(album.getHostId() == this.getLoginUser().getHost().getId()) {
			outJson(photoService.selectPhotoByAlbumKey(album.getId()));
		}
		outJson(this.getErrRes());
	}
	
	//创建照片
	@Action(value="create")
	public void createPhoto() {
		try {
			if(photo.getAlbumId() == null) {
				photo.setHostId(this.getLoginUser().getHost().getId());
			}
			photoService.insertPhoto(photo);
			res.put("photo", photo);
		} catch (IOException e) {
			log.error("创建照片失败", e);
			res = this.getErrRes("未知原因图片创建失败.");
		}
		outJson(res);
	}
	//编辑照片信息
	@Action(value="save")
	public void savePhoto() {
		photoService.updatePhoto(photo);
		outJson(res);
	}
	//删除照片
	@Action(value="del")
	public void delPhoto() {
		photoService.deletePhoto(id, true);
		outJson(res);
	}
	
	//保存相册
	@Action(value="saveAlbum")
	public void createAlbum() {
		if(album.getId() == null) {
			album.setHostId(this.getLoginUser().getHost().getId());
			photoService.insertPhotoAlbum(album);
			res.put("album", album);
		} else {
			photoService.updatePhotoAlbum(album);
		}
		outJson(res);
	}
	
	//删除相册
	@Action(value="delAlbum")
	public void delAlbum() {
		HostPhotoAlbum album = photoService.selectPhotoAlbumByKey(id);
		if(album.getHostId() == this.getLoginUser().getHost().getId()) {
			photoService.deletePhotoAlbum(id);
		} else {
			res = this.getErrRes();
		}
		outJson(res);
	}
	
	//设置封面
	@Action(value="cover")
	public void cover() {
		photoService.updatePhotoAlbumCover(id);
		outJson(res);
	}

	public HostPhoto getPhoto() {
		return photo;
	}

	public void setPhoto(HostPhoto photo) {
		this.photo = photo;
	}

	public HostPhotoAlbum getAlbum() {
		return album;
	}

	public void setAlbum(HostPhotoAlbum album) {
		this.album = album;
	}
}
